#include "imcommon.h"

char *clear_msgcontent_symbol(char *source)
{
	if (source == NULL)
		return NULL;
	char *str = strdup(source);
	int result;
	if (result != -1)
		result = cns_str_ereplace(&str, "\\\\n", "\n");
	if (result != -1)
		result = cns_str_ereplace(&str, "\\\\t", "\t");

	return str;
}

int write_yahooMsg_Login(MsgNode *node)
{
    /*
    {"sessionId":"hjuD7fXs3B6Kw236_dHu.nRn9_Hdk.1mbdo2","primaryLoginId":"xiyingit@yahoo.cn","contacts":[{"
    contact":{"id":"fengjie917","uri":"","groups":[{"group":"Friends","name":"Friends","uri":""},{"group":"\u597d\u53cb","
    name":"\u597d\u53cb","uri":""}]}}],"displayInfo":{"avatarPreference":"0"},"mpopInfo":{"state":0,"resourceId":1,"
    totalEndpoints":1},"notifyServerToken":{"token":"
    2KjLCmFfBf8t60IBRCVlMo9IqLTmwRUc70b8CSfpJCrvudDQId8yKbSaH_2xZIWhK9Iahsyw7f3ybJ5E-|EWr_785epnoWt7lNl7fZ.g--","
    expires":1345199348},"server":"prod2.rest-core.msg.yahoo.com","notifyServer":"prod1.rest-notify.msg.yahoo.com
      */

    char *pstart = NULL;
    char *pend  = NULL;
    char *tmp    = NULL;

    static int friend_flag = 0;

    if (NULL != node->body)
        
    pstart = strstr(node->body,"primaryLoginId\":\"");
    if (NULL == pstart)
        return -1;
    pstart += 17;
    pend = strstr(pstart,"\",\"");
    if (NULL == pend)
        return -1;
    strncpy(node->username,pstart,pend-pstart);
    node->username[pend-pstart+1] = '\0';
    if (!strstr(node->username,"@yahoo.cn"))
        strcat(node->username,"@yahoo.com.cn");

    usr_record *p = search_user(user_record,node->srcMac, "@yahoo");
    if (p != NULL)
        delete(p);
    free(p);
    p = NULL;
    insert_user(user_record,node->srcMac, node->username);

    pstart = strstr(pend, "contact\":{\"id\":\"");
    if (NULL != pstart)
    {
        pstart += 16;
        pend = strstr(pstart,"\",");
        if (NULL == pend)
            return -1;
        node->friends = (char *)malloc(MAX_TO_LEN+1);
        
        
        friend_flag = 1;
      
        memset(node->friends, 0, MAX_TO_LEN);
        strncpy(node->friends,pstart,pend-pstart);
        node->friends[pend-pstart+1] = '\0';
        if (!strstr(node->friends,"@yahoo.cn"))
        {
            //strcat(node->friends,"@yahoo.com.cn");
            memcpy(node->friends+(pend-pstart),"@yahoo.com.cn", 13 );
        }

    }

    node->msgType = Login;
    output_msg(node);
    if (1 == friend_flag)
    {
        free(node->friends);
        node->friends = NULL;
        friend_flag = 0;
    }
    return 0;
}

int write_yahooMsg_Send(MsgNode *node)
{
     /*
    {"convId":"xiyingit@yahoo.cn~fengjie917~yahoo",
    "txnId":"xiyingit@yahoo.cn~1345015274718~3",
    "message":"<font face=\"Arial\" size=\"10\">hahahahaha</font>","sendAs":"xiyingit@yahoo.cn"}
      */
    char *pstart = NULL;
    char *pend = NULL;
   // printf("data:%s\n",node->data);
    pstart = strstr(node->data,"{\"convId\":\"");
    if (NULL == pstart)
        return -1;
    pstart += 11;
    pend = strstr(pstart,"~");
    if (NULL == pend)
        return -1;
    strncpy(node->from,pstart,pend-pstart);
    node->from[pend-pstart+1] = '\0';
    if (!strstr(node->from,"@yahoo.cn"))
        strcat(node->from,"@yahoo.com.cn");
    strcpy(node->username,node->from);


    pend += 1;    
    pstart = strstr(pend,"~yahoo");
    if (NULL == pstart)
        return -1;
    strncpy(node->to,pend,pstart-pend);
    node->to[pstart-pend+1] = '\0';
    if (!strstr(node->to,"@"))
        strcat(node->to,"@yahoo.com.cn");

    pend = strstr(pstart,"message\":\"");
    if (NULL == pend)
        return -1;
    pend += 10;
    pstart= strstr(pend,"\",\"sendAs");
    if (NULL == pstart)
        return -1;
    node->content = (char *)malloc(pstart-pend+1);
    memset(node->content,0,pstart-pend+1);
    strncpy(node->content,pend,pstart-pend);
    node->content[pstart-pend+1] = '\0';
    strcpy(node->content,clear_html_tag(node->content));
    clear_tag(node->content);
    strcpy(node->content,clear_msgcontent_symbol(node->content));
   // printf("node->content:%s\n",node->content);
  /*
    int result = yahooMsg_str_convert(node->content, pend-pstart);
    if (result == -1)
	return -1;
	*/

    node->msgType = Text;
    node->s_or_r = 0;
    output_msg(node);
    return 0;
}

int write_yahooMsg_Rec(MsgNode *node)
{
    /*
    { "@mpopState" : 0, "@pendingMsg" : 0, "@syncStatus" : 0, "responses" : [ { "message" : { "status" : 1, "sequence" : 12, "
    sender" : "fengjie917" , "receiver" : "xiyingit@yahoo.cn" , "msg" : "<font face=\"Arial\" size=\"10\">gaergrag <\/font>" , "
    timeStamp" : 1345092963, "hash" : "YW5R4nORaVBrKz70Z4alJW27t+2sbw==" , "
    msgContext" : "YW5R4nORaVBrKz70Z4alJW27t+2sbw=="  } }
 ] }
      */     
    char *pstart     = NULL;
    char *pend      = NULL;
    //char *strtmp = NULL;
    //printf("data:%s\n",node->data);
    //char patternrecive[] = "\"message\" : (.*?)  } }\n";
  //  regcompile_2(node->data, patternrecive, &strtmp);
    pstart = strstr(node->data, "\"message\"");
    if (NULL == pstart)
        return -1;
    
    pstart = strstr(pstart, "sender\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 11;
    pend = strstr(pstart,"\" , \"");
    if (NULL == pend)
        return -1;
    strncpy(node->from,pstart,pend-pstart);
    node->from[pend-pstart+1] = '\0';
    if (!strstr(node->from,"@yahoo.cn"))
        strcat(node->from,"@yahoo.com.cn");
    
    pstart = strstr(pend,"receiver\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 13;
    pend = strstr(pstart,"\" , \"");
    if (NULL == pend)
        return -1;
    strncpy(node->to,pstart,pend-pstart);
    node->to[pend-pstart+1] = '\0';
    if (!strstr(node->to,"@yahoo.cn"))
        strcat(node->to,"@yahoo.com.cn");

    strcpy(node->username,node->to);

    pstart = strstr(pend,"msg\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 8;
    pend = strstr(pstart,"\" , \"");
    if (NULL == pend)
        return -1;
    node->content = (char *)malloc(pend-pstart+1);
    memset(node->content,0,pend-pstart+1);
    strncpy(node->content,pstart,pend-pstart);
    node->content[pend-pstart+1] = '\0';
    char *tmp_content = clear_html_tag(node->content);
    OUT_PUT;
    free(node->content);
    node->content = clear_msgcontent_symbol(tmp_content);
    
    node->s_or_r = 1;
    output_msg(node);
   // free(strtmp);
    free(tmp_content);
    tmp_content = NULL;
    return 0;
}

int write_yahooMsg_Logout(MsgNode *node)
{
    /*{ "@mpopState" : 0, "@pendingMsg" : 0, "@syncStatus" : 0, "responses" : [ { "logOff" : { "status" : 1, "sequence" : 5, "
    buddy" : "xiyingit@yahoo.cn"  } }
    ] }
      */
    node->msgType = Logout;

    char *pstart = NULL;
    char *pend  = NULL;

    pstart = strstr(node->data, "\"buddy\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 11;
    pend = strstr(pstart, "\"  }");
    strncpy(node->username,pstart,pend-pstart);
    node->username[pend-pstart+1] = '\0';

    printf("%s exited\n",node->username);
    struct tm *ptm = localtime(&(node->time));
    printf("exited time is:%u-%u-%u %u:%02u\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,ptm->tm_hour, ptm->tm_min);
    output_msg(node);
    return 0;
}

int write_yahooMsg_Logout2(MsgNode * node)
{
    char *pstart = NULL;
    char *pend  = NULL;

    if (NULL==node->body && NULL==node->data)
        return -1;

    node->msgType = Logout;
    
    usr_record *p = search_user(user_record,node->srcMac, "@yahoo");
    if (NULL != p)
    {
        memcpy(node->username,p->username, strlen(p->username)+1);
        del_user(p);
    }
    output_msg(node);
    
    return 0;
}

